from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Date

import env

base = declarative_base()

class User(base):
    '''Objet User'''
    
    __tablename__ = "user"

    id = Column(Integer, primary_key=True)
    name = Column(String(40))
    fullname = Column(String(40))

    def __init__(self, fullname, name):
        self.name = name
        self.fullname = fullname
        
    def __repr__(self):
        return "<User(Id : '%s', '%s' '%s')>" % (self.id, self.name, self.fullname)


class Team(base):
    '''Objet Team'''
    
    __tablename__ = "team"

    id = Column(Integer, primary_key=True)
    id_team = Column(Integer)
    id_user = Column(Integer)

    def __init__(self, id_team, id_user):
        self.id_team = id_team
        self.id_user = id_user
        
    def __repr__(self):
        return "<Team(Team :'%s')>" % (self.id_team)

class TeamSet(base):
    '''Objet Team'''
    
    __tablename__ = "team_set"

    id = Column(Integer, primary_key=True)
    id_team_set = Column(Integer)
    id_team = Column(Integer)

    def __init__(self, id_team_set, id_team):
        self.id_team_set = id_team_set
        self.id_team = id_team
        
    def __repr__(self):
        return "<TeamSet(TeamSet :'%s')>" % (self.id_team_set)

class Assignment(base):
    '''Objet Assignment'''
    
    __tablename__ = "assignment"

    id = Column(Integer, primary_key=True)
    id_template = Column(Integer)
    date = Column(Date)
    id_teamset = Column(Integer)
    id_user = Column(Integer)

    def __init__(self, id_template, date, id_teamset, id_user):
        self.id_template = id_template
        self.id_teamset = id_teamset
        self.date = date
        self.id_user = id_user
            
    def __repr__(self):
        return "<Assignment(Id teamplate :'%s', Date : '%s', Id teamset : '%s', Id user : '%s')>" % (self.id_template, self.date, self.id_teamset, self.id_user)

    def get_template(self):
        return env.session.query(Template).filter(Template.id == self.id_template).first()
    
    def get_teamset(self):
        return env.session.query(TeamSet).filter(TeamSet.id_team_set == self.id_teamset).first()


class Template(base):
    '''Objet Template'''

    __tablename__ = "template"

    id = Column(Integer, primary_key=True)
    title = Column(String(40))

    def __init__(self, title):
        self.title = title

    def __repr__(self):
        return "<Template(Title : '%s')>" % (self.title)
